from flask import g
from flask_restful import Resource, reqparse, abort, marshal, fields

from App.apis.api_constant import HTTP_CREATE_OK
from App.apis.cinema_admin.utils import login_required, required_permission
from App.ext import db
from App.models.cinema_admin.cinema_address_model import CinemaAddress
from App.models.cinema_admin.cinema_user_model import CinemaUser
from App.models.cinema_admin.permissions_constant import PERMISSION_WRITE

parse = reqparse.RequestParser()
parse.add_argument("name" , required = True , help = '请提供影院名字')
parse.add_argument("city" , required = True , help = '请提供城市')
parse.add_argument("district" , required = True , help = '请提供城市所在区')
parse.add_argument("address" , required = True , help = '请提供详细地址')
parse.add_argument("phone" , required = True , help = '请提供电影院的联系方式')

cinema_fields = {
    'c_user_id':fields.Integer,
    'name':fields.String,
    'city':fields.String,
    'district':fields.String,
    'address':fields.String,
    'phone':fields.String(32),
    'score':fields.Float,
    'hallnum':fields.Integer,
    'astrict':fields.Float,
    'flag':fields.Boolean,
}

class CinemaAddressesResource(Resource):
    def get(self):
        return {'msg':'get is ok'}

    @required_permission(PERMISSION_WRITE)
    def post(self):
        args = parse.parse_args()
        name = args.get('name')
        city = args.get('city')
        district = args.get('district')
        address = args.get('address')
        phone = args.get('phone')

        user = CinemaUser.query.filter(CinemaUser.phone == phone).first()
        print('=======', user ,'=======')
        cinema_address = CinemaAddress()
        cinema_address.C_user_id = user.id        #???
        cinema_address.name = name
        cinema_address.city = city
        cinema_address.district = district
        cinema_address.address = address
        cinema_address.phone = phone
        # db.session.add(cinema_address)    # basemodel已经封装了save方法，就不用再写了
        # db.session.commit()
        if not cinema_address.save():
            abort(400 , msg = "cinema can't save")

        data = {
            'status':HTTP_CREATE_OK,
            'msg':'cinema create ok',
            'data':marshal(cinema_address , cinema_fields)
        }
        return data





class CinemaAddressResource(Resource):
    def get(self , id):
        pass

    def put(self , id):
        pass

    def patch(self):
        pass

    def delete(self):
        pass
